//
//  VIPVoiceViewController.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/7.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "VIPVoiceViewController.h"
#import "VIPVideoPictureCell.h"
@interface VIPVoiceViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *stateCollectionView; // 模式列表
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, strong) NSMutableArray *dataArray; 
@end

@implementation VIPVoiceViewController
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageIndex = 1;
}

- (void)setupUI
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(20, 16, 20, 16);
    layout.minimumLineSpacing = 7;
    layout.minimumInteritemSpacing = 6;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.stateCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.stateCollectionView.delegate = self;
    self.stateCollectionView.dataSource = self;
    self.stateCollectionView.backgroundColor = UIColor.clearColor;
    self.stateCollectionView.showsHorizontalScrollIndicator = NO;
    [self.stateCollectionView registerClass:[VIPVideoPictureCell class] forCellWithReuseIdentifier:@"VIPVideoPictureCell"];
    [self.view addSubview:self.stateCollectionView];
    CGFloat heights = Height_NavBar + 52;
    [self.stateCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.offset(-heights);
    }];
    @weakify(self)
    self.stateCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.pageIndex = 0;
        [self requestData];
    }];
    self.stateCollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageIndex ++;
        [self requestData];
    }];
    [self requestData];
}

- (void)requestData
{
    NSDictionary *paramDic = @{
        @"type":@(2),
        @"sort_id":@(0),
        @"keyword":@"",
        @"page":@(self.pageIndex),
        @"limit":@(10)
    };
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypeGet URLString:@"material" parameters:paramDic done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if (self.pageIndex == 0) {
            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray:responseObject[@"data"][@"list"][@"list"]];
        } else {
            [self.dataArray addObjectsFromArray:responseObject[@"data"][@"list"][@"list"]];
        }
        [self.stateCollectionView.mj_header endRefreshing];
        if (self.pageIndex == [responseObject[@"data"][@"list"][@"page_sum"] integerValue]) {
            [self.stateCollectionView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.stateCollectionView.mj_footer endRefreshing];
        }
        [self.stateCollectionView reloadData];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [self.stateCollectionView.mj_header endRefreshing];
        [self.stateCollectionView.mj_footer endRefreshing];
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((kDeviceWidth - 32 - 7)/2, 108);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VIPVideoPictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VIPVideoPictureCell" forIndexPath:indexPath];
    cell.dataDic = self.dataArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self pushVideoDetailVCWithID:self.dataArray[indexPath.row][@"id"]];
}

#pragma mark - JXCategoryListContentViewDelegate

- (UIView *)listView {
    return self.view;
}

/**
 可选实现，列表将要显示的时候调用
 */
- (void)listWillAppear
{
    
}

@end
